package ObjectFile;

/**
 * The linking record class will create the linking portion of the object file.
 * 
 * @author yogesh
 */
public class LinkingRecord {
    public static String[][] linking_record_array = new String[0][0];
    IceAssembly.Table table_instance = new IceAssembly.Table();
    
    /**
    * The purpose of this function is to find the location counter of a desired label.  
      * The function will search through the current symbol table until a match is found and will return the location of that match.
    * <p>
    * <b>Change Log:</b><br>
    * 5/4/08 - Initial Code Entry - Yogesh Jindal<br>
      * 5/5/08 - Inserted Documentation - Keith Hill <br>
      * 5/9/08 - Changed type to public - needed by other package - Yogesh Jindal.<br>
    * <p>
    * <b>Error Conditions Tested:</b><br>
    * None <br>
    * <p>
    * <b>Date of module being installed:</b> 5/4/08
    * <p><b>Original Author:</b>  <original author>
    * @param label This is the label that the caller needs the location of.
    * @return The function will return the location of label or -100 if the label is not found.
    */
    public String getLcOfLabel(String[][] symbol_table, String label, int num_row){
        for(int index=0; index<num_row; index++){
            if(symbol_table[index][0].compareTo(label)==0 && symbol_table[index][2].compareTo("EQU")==0){
                return symbol_table[index][3];
            }
        }        
        for(int index=0; index<num_row; index++){
            if(symbol_table[index][0].compareTo(label)==0 && !symbol_table[index][2].contentEquals("SHVAR") && !symbol_table[index][2].contentEquals("EXVAR")){
                return symbol_table[index][1];
            }
        }
        return "-100";
    }
    
    /**
    * The iniSymbolArray function will accept an incomming array and will create a symbol array to be placed in the object file.
    * <p>
    * <b>Change Log:</b><br>
    * 5/13/08 - Added documentation - Keith Hill<br>
    * <p>
    * <b>Error Conditions Tested:</b><br>
    * None <br>
    * <p>
    * <b>Date of module being installed:</b> 5/10/08
         * @param symbol_table This is the incomming string array.
         * @param num_row This is the incomming number of row.
    * <p><b>Original Author:</b>  Yogesh Jindal
    */
    public void iniSymbolArray(String[][] symbol_table, int num_row){
        int linking_array_count = 0;
        linking_array_count=0;
        for(int index=0; index<num_row; index++){
            if(symbol_table[index][2].contentEquals("START") || symbol_table[index][2].contentEquals("RESET") || (symbol_table[index][2].contentEquals("SHVAR")&& !getLcOfLabel(symbol_table, symbol_table[index][0], num_row).contentEquals("-100"))){
                linking_array_count++;
            }
        }
        int temp=0;
        linking_record_array = new String[linking_array_count][3];
        for(int index=0; index<num_row; index++){
            if(symbol_table[index][2].contentEquals("START") || symbol_table[index][2].contentEquals("RESET")){
                linking_record_array[temp][0] = symbol_table[index][0];
                linking_record_array[temp][1] = symbol_table[index][1];
                linking_record_array[temp][2] = symbol_table[index][2];
                temp++;
            }
            else if(symbol_table[index][2].contentEquals("SHVAR")){
                String value = getLcOfLabel(symbol_table, symbol_table[index][0],num_row);
                if(!value.contentEquals("-100")){
                    linking_record_array[temp][0] = symbol_table[index][0];
                    linking_record_array[temp][1] = value;
                    linking_record_array[temp][2] = symbol_table[index][2];
                    temp++;
                }else{
                    System.out.println("Severe Error: The label "+symbol_table[index][0]+" was found declared as SHVAR but" +
                            " was not defined anywhere in this program. Label ignored.");
                    System.out.println("Corrective Action: Define this label");
                    //means a label is declared as shvar but is not defined anywhere else.
                }
            }
        } 
    }          
}